package com.ll.gulimall.order.web;

import com.ll.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Controller
public class HelloController {

    @Autowired
    RabbitTemplate rabbitTemplate;


    @GetMapping("/{page}.html")
    public String page(@PathVariable String page) {
        return page;
    }

    @GetMapping("/testSend")
    @ResponseBody
    public String testSend() {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setId(123L);
        orderEntity.setCreateTime(new Date());

        rabbitTemplate.convertAndSend("order-event-exchange", "order.create.order", orderEntity, new CorrelationData("123"));
        return "ok";

    }

    //@RabbitListener(queues = "order.release.order")
    public void rabbitListener(Message message, OrderEntity order, Channel channel) throws IOException {
        System.out.println("过期订单已收到:" + order);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

    }
}
